namespace( "org.jaxson.util" );

/**
 * Ensures only 1 function will execute in the scope of
 * a single Timer object.  In effect this behavior is
 * similar to a thread.
 */
org.jaxson.util.Timer = function( func, delay ){
	
	var id = -1;
	var running = false;
	var wrapper = function(){
		return func( arguments );
	};

	return{
		getId : function(){ 
			return id; 
		},
		start: function(){
			
			if( running )
				return;
			
			clearTimeout( id );
			wrapper();
			id = setInterval( wrapper, delay );
			running = true;
			return id;
		},
		stop: function(){
			clearInterval(id);
			id = -1;
			running = false;
		},
		isRunning : function(){
			return running; 
		},
		setFunction: function( func ){
			if( running )
				this.stop();
			
			wrapper = function(){
				func(arguments);
			};
			
			return id;
		}
	};
};
